/*
package com.hongyi.serivice.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.hongyi.pojo.User;
import com.hongyi.serivice.UserService;
import com.hongyi.utils.HttpUtils;
import com.hongyi.utils.HttpUtils1;
import com.hongyi.vo.ApiResponse;
import com.hongyi.vo.ApiResponseList;
import com.hongyi.vo.UserVo;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpResponse;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

*/
/**
 * @Author: hongyi
 * @CreateTime: 2024-10-22 15:03:00
 *//*



@Service
@Log4j2
public class UserServiceImpl1 implements UserService {
    private final RestTemplate restTemplate = new RestTemplate();
    private final ObjectMapper objectMapper = new ObjectMapper();


    @Override
    public List<UserVo> findALl() {
        String host = "http://127.0.0.1:9091/";
        String path = "user/findAll";
        String method = "GET";


        try {
            HttpResponse response = HttpUtils1.doGet(host, path, method);
            if (response.getStatusLine().getStatusCode() == 200) {
                // 将响应体转换为字符串
                // 将 JSON 字符串转换为 ApiResponseList 对象
                String json = EntityUtils.toString(response.getEntity());
                // 将 JSON 字符串转换为 ApiResponseList 对象
                ApiResponseList responseList = objectMapper.readValue(json, ApiResponseList.class);
                List<UserVo> userVO = responseList.getData();
                return userVO;
            } else {
                throw new RuntimeException("请求失败，状态码：" + response.getStatusLine().getStatusCode());
            }

        } catch (Exception e) {
            // 在这里添加日志记录
            // logger.error("获取用户信息失败", e);
            throw new RuntimeException("获取用户信息失败", e); // 抛出更明确的异常
        }
    }

    @Override
    public UserVo getById(Long id) {
        String host = "http://127.0.0.1:9091/";
        String path = "user/" + id;
        String method = "GET";


        try {
            // 发送 GET 请求
            HttpResponse response = HttpUtils1.doGet(host, path, method);

            // 检查 HTTP 响应状态码
            if (response.getStatusLine().getStatusCode() == 200) { // 如果状态码为 200，表示请求成功
                // 将响应体转换为字符串
                String json = EntityUtils.toString(response.getEntity());

                // 将 JSON 字符串转换为 ApiResponse 对象
                ApiResponse apiResponse = objectMapper.readValue(json, ApiResponse.class);
                // 从 ApiResponse 中获取 UserVo 对象
                UserVo userVo = apiResponse.getData(); // 根据 ApiResponse 的结构，可能需要调整
                return userVo;
            } else {
                throw new RuntimeException("请求失败，状态码：" + response.getStatusLine().getStatusCode());
            }

        } catch (Exception e) {
            // 在这里添加日志记录
            // logger.error("获取用户信息失败", e);
            throw new RuntimeException("获取用户信息失败", e); // 抛出更明确的异常
        }
    }


    @Override
    public void save(User user) {
        String host = "http://127.0.0.1:9091/";
        String path = "user/insert";
        String method = "POST";

        try {
            Gson gson = new Gson();
            String requestBody = gson.toJson(user);
            HttpResponse response = HttpUtils1.doPost(host, path, method, requestBody);

            if (response.getStatusLine().getStatusCode() == 200) {
                System.out.println("保存成功");
            }
            // 处理响应体（可选）
            String jsonResponse = EntityUtils.toString(response.getEntity());
            System.out.println(jsonResponse);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void update(User user) {
        // 确保 user 不为 null
        if (user == null) {
            System.out.println("用户信息不能为空");
            return;
        }

        String host = "http://127.0.0.1:9091/";
        String path = "user/update"; // 修正路径拼接
        String method = "PUT";


        try {
            Gson gson = new Gson();
            String requestBody = gson.toJson(user); // 仅将 user 对象转换为 JSON
            // 发送 PUT 请求
            HttpResponse response = HttpUtils1.doPut(host, path, method, requestBody);

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                System.out.println("修改成功！！！");
            } else {
                System.out.println("修改失败，状态码: " + statusCode);
            }

            // 处理响应内容
            String jsonResponse = EntityUtils.toString(response.getEntity());
            System.out.println("响应内容: " + jsonResponse);

        } catch (IOException e) {
            System.out.println("请求失败: " + e.getMessage());
            e.printStackTrace(); // 打印完整的堆栈信息
        } catch (Exception e) {
            System.out.println("发生意外错误: " + e.getMessage());
            e.printStackTrace(); // 打印完整的堆栈信息
        }
    }


    @Override
    public void delete(Long id) {
        String host = "http://127.0.0.1:9091/";
        String path = "user/delete/" + id;
        String url = host + path;
        String method = "DELETE";

        try {
            HttpResponse response = HttpUtils1.doDelete(host, path, method);

            if (response.getStatusLine().getStatusCode() == 200) {
                System.out.println("删除成功");
            }
            // 处理响应体（可选）
            String jsonResponse = EntityUtils.toString(response.getEntity());
            System.out.println(jsonResponse);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public List<UserVo> findName(String name) throws Exception {
        //
        // http://127.0.0.1:9091/user/findByName?name=A
        // 构建请求 URL
        String host = "http://127.0.0.1:9091/";
        String path = new URIBuilder("user/findByName2").addParameter("name", name).build().toString();
        //  String path = "user/findByName2?name=" + name;
        //   String path = "user/findByName2?name=" + URLEncoder.encode(name, "UTF-8");
        String url = host + path;
        String method = "GET";
        try {
            // 发送 GET 请求
            HttpResponse response = HttpUtils1.doGet(host, path, method);

            // 检查 HTTP 响应状态码
            if (response.getStatusLine().getStatusCode() == 200) { // 如果状态码为 200，表示请求成功
                // 将响应体转换为字符串
                String json = EntityUtils.toString(response.getEntity());

                // 将 JSON 字符串转换为 ApiResponse 对象
                ApiResponseList apiResponse = objectMapper.readValue(json, ApiResponseList.class);
                // 从 ApiResponse 中获取 UserVo 对象
                List<UserVo> userVos = apiResponse.getData();
                return userVos;
            } else {
                throw new RuntimeException("请求失败，状态码：" + response.getStatusLine().getStatusCode());
            }

        } catch (Exception e) {
            // 在这里添加日志记录
            // logger.error("获取用户信息失败", e);
            throw new RuntimeException("获取用户信息失败", e); // 抛出更明确的异常
        }
    }

    @Override
    public List<UserVo> findUser(User user) throws Exception {
        String host = "http://127.0.0.1:9091/";
        // String path = "user/findUser";

        String path = new URIBuilder("user/findByUserVo")
                .addParameter("name", user.getName())
                .addParameter("email", user.getEmail())
                .build().
                toString();
        String method = "GET";

        try {
            // 发送 GET 请求
            HttpResponse response = HttpUtils1.doGet(host, path, method);

            // 检查 HTTP 响应状态码
            if (response.getStatusLine().getStatusCode() == 200) { // 如果状态码为 200，表示请求成功
                // 将响应体转换为字符串
                String json = EntityUtils.toString(response.getEntity());

                // 将 JSON 字符串转换为 ApiResponse 对象
                ApiResponseList apiResponse = objectMapper.readValue(json, ApiResponseList.class);
                // 从 ApiResponse 中获取 UserVo 对象
                List<UserVo> userVos = apiResponse.getData();
                return userVos;
            } else {
                throw new RuntimeException("请求失败，状态码：" + response.getStatusLine().getStatusCode());
            }

        } catch (Exception e) {
            // 在这里添加日志记录
            // logger.error("获取用户信息失败", e);
            throw new RuntimeException("获取用户信息失败", e); // 抛出更明确的异常
        }

    }

}
*/
